package com.linko.intelligent.sys.core.controller;

import com.github.pagehelper.PageInfo;
import com.linko.intelligent.base.CommonResponse;
import com.linko.intelligent.base.UserInfo;
import com.linko.intelligent.sys.core.service.RoleService;
import com.linko.intelligent.sys.core.service.UserService;
import com.linko.intelligent.sys.pojo.dto.LoginDto;
import com.linko.intelligent.sys.pojo.dto.UserDto;
import com.linko.intelligent.sys.pojo.po.User;
import com.linko.intelligent.sys.pojo.query.UserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController", tags = "用户API")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;


    /***
     * 新增
     * @param loginDto
     * @return
     */
    @ApiOperation("登陆")
    @PostMapping(value = "/login")
    public CommonResponse<UserInfo> login(@RequestBody LoginDto loginDto) {
        UserInfo userInfo = userService.login(loginDto);
        return new CommonResponse<UserInfo>(CommonResponse.SUCCESS, "登陆成功", userInfo);
    }

    /***
     * 新增
     * @param dto
     * @return
     */
    @ApiOperation("新增")
    @PostMapping(value = "/save")
    public CommonResponse save(@RequestBody UserDto dto) {
        userService.save(dto);
        return new CommonResponse(CommonResponse.SUCCESS, "保存成功");
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/{id}/del")
    public CommonResponse del(@PathVariable("id") Long id) {
        userService.delete(id);
        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
    }

    @ApiOperation("修改")
    @PutMapping(value = "/{id}/update")
    public CommonResponse update(@PathVariable("id") Long id, @RequestBody UserDto dto) {
        userService.update(id, dto);
        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
    }

    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public CommonResponse<User> get(@PathVariable("id") Long id) {
        User po = userService.getById(id);
        return new CommonResponse<User>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("详情-关联内容")
    @GetMapping(value = "/{id}/detail")
    public CommonResponse<UserInfo> detail(@PathVariable("id") Long id) {
        UserInfo po = userService.getDetail(id);
        return new CommonResponse<UserInfo>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("列表")
    @GetMapping(value = "/list")
    public CommonResponse<List<User>> list(@SpringQueryMap UserQuery query) {
        List<User> list = userService.queryList(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功", list);
    }

    /***
     * @param query
     * @return
     */
    @ApiOperation("分页")
    @GetMapping(value = "/page")
    public CommonResponse<PageInfo<User>> page(@SpringQueryMap UserQuery query) {
        PageInfo<User> pageInfo = userService.queryPage(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功!", pageInfo);
    }

    /***
     * 根据名字找ID
     * @return
     */
    @ApiOperation("根据名字找ID")
    @PostMapping(value = "/find/{name}")
    public CommonResponse<Long> findIdByName(@RequestBody List<User> all, @PathVariable("name") String name) {
        Long id = userService.findIdByName(all, name);
        return new CommonResponse<Long>(CommonResponse.SUCCESS, "成功", id);
    }


}
